﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using System.Windows.Forms;
using TSVNAddin.Config;

namespace TSVNAddin.Core.Test
{
	[TestFixture]
	public class AboutAddinCommandFixture
	{
		[Test]
		public void Exec()
		{
			var config = new AddinConfig()
			{
				AboutOnStart = true
			};

			var aboutView = MockRepository.GenerateMock<IAboutView>();
			aboutView.Stub(x => x.ShowDialog()).Return(DialogResult.OK);
			aboutView.Stub(x => x.ShowOnStart).Return(false);

			var factory = MockRepository.GenerateMock<IDialogFactory>();
			factory.Stub(x => x.CreateAboutDialog()).Return(aboutView);

			var configStorage = MockRepository.GenerateMock<ConfigStorage>((XmlFileStorage)null);
			configStorage.Stub(x => x.LoadAddinConfig()).Return(config);

			var command = new AboutAddinCommand(factory, configStorage);
			command.Execute();

			Assert.AreEqual(false, config.AboutOnStart);
			configStorage.AssertWasCalled(x => x.StoreAddinConfig(config));
			aboutView.AssertWasCalled(x => x.Dispose());
		}

		[Test]
		public void ExecWhenCancelDialog()
		{
			var config = new AddinConfig()
			{
				AboutOnStart = true
			};

			var aboutView = MockRepository.GenerateMock<IAboutView>();
			aboutView.Stub(x => x.ShowDialog()).Return(DialogResult.Cancel);
			aboutView.Stub(x => x.ShowOnStart).Return(false);

			var factory = MockRepository.GenerateMock<IDialogFactory>();
			factory.Stub(x => x.CreateAboutDialog()).Return(aboutView);

			var configStorage = MockRepository.GenerateMock<ConfigStorage>((XmlFileStorage)null);
			configStorage.Stub(x => x.LoadAddinConfig()).Return(config);

			var command = new AboutAddinCommand(factory, configStorage);
			command.Execute();

			Assert.AreEqual(true, config.AboutOnStart);
			configStorage.AssertWasNotCalled(x => x.StoreAddinConfig(config));
			aboutView.AssertWasCalled(x => x.Dispose());
		}

		[Test]
		public void ShowAboutDialogOnStart()
		{
			var config = new AddinConfig()
			{
				AboutOnStart = true
			};

			var aboutView = MockRepository.GenerateMock<IAboutView>();
			aboutView.Expect(x => x.ShowDialog()).Return(DialogResult.OK);
			aboutView.Stub(x => x.ShowOnStart).Return(false);

			var factory = MockRepository.GenerateMock<IDialogFactory>();
			factory.Stub(x => x.CreateAboutDialog()).Return(aboutView);

			var configStorage = MockRepository.GenerateMock<ConfigStorage>((XmlFileStorage)null);
			configStorage.Stub(x => x.LoadAddinConfig()).Return(config);

			var command = new AboutAddinCommand(factory, configStorage);
			command.ShowAboutDialogOnStart();

			aboutView.VerifyAllExpectations();
		}

		[Test]
		public void DoesntShowAboutDialogOnStart()
		{
			var config = new AddinConfig()
			{
				AboutOnStart = false
			};

			var aboutView = MockRepository.GenerateMock<IAboutView>();
			aboutView.Expect(x => x.ShowDialog()).Repeat.Never();
			aboutView.Stub(x => x.ShowOnStart).Return(false);

			var factory = MockRepository.GenerateMock<IDialogFactory>();
			factory.Stub(x => x.CreateAboutDialog()).Return(aboutView);

			var configStorage = MockRepository.GenerateMock<ConfigStorage>((XmlFileStorage)null);
			configStorage.Stub(x => x.LoadAddinConfig()).Return(config);

			var command = new AboutAddinCommand(factory, configStorage);
			command.ShowAboutDialogOnStart();

			aboutView.VerifyAllExpectations();
		}
	}
}
